package java_test.nettest;

import java.io.IOException;
import java.net.*;

public class UDPSenderTest {
    public static void main(String[] args) {
        UDPSender();
    }
    
    public static void UDPSender() {
        DatagramSocket datagramSocket = null;
        try {
            datagramSocket = new DatagramSocket();
            String str = "我是UDP发送的数据";
            DatagramPacket datagramPacket = new DatagramPacket(str.getBytes(), 0, str.length() ,InetAddress.getLocalHost(), 9090);
            datagramSocket.send(datagramPacket);
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (datagramSocket != null) {
                datagramSocket.close();
            }
        }
    }
}
